package com.freshcity.page.feiji
{
	import com.freshcity.component.container.Component;
	import com.freshcity.core.MainScene;
	import com.freshcity.core.OpCodes;
	import com.freshcity.feiji.feijiRankWin;
	import com.greensock.TweenMax;
	
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.text.StyleSheet;
	import flash.text.TextField;
	import flash.utils.Dictionary;
	
	public class FeijiRank extends Component
	{
		private var _data:*;
		private var back:feijiRankWin;
		
		private var feijiDict:Dictionary = new Dictionary();
		private var starFeiji:StarFeiji;
		private var style:StyleSheet;
		
		public function FeijiRank(parent:DisplayObjectContainer=null, xpos:Number=0, ypos:Number=0)
		{
			this.visible = false;
			starFeiji = new StarFeiji();
			style = new StyleSheet();
			style.setStyle("a:link", {color: '#0000FF', fontSize: '12'});
			style.setStyle("a:hover", {color: '#ff0000', textDecoration: 'underline', fontSize: '12'});
			style.setStyle("a:visited",{color:'#990000',fontSize:'12'});
			
			super(parent, xpos, ypos);
		}
		
		override protected function addChildren():void
		{
			super.addChildren();
			back = new feijiRankWin();
			addChild(back);
			//addChild(starFeiji);
		}
		
		override public function draw():void
		{
			super.draw();
			starFeiji.main = main;
		}
		
		protected function onLinkHandler(event:TextEvent):void
		{
			starFeiji.data = feijiDict[event.text];
			starFeiji.show();
		}
		
		public function get data():*
		{
			return _data;
		}

		public function set data(value:*):void
		{
			_data = value;
			this.resort();
		}
		
		private function resort():void
		{
			var i:int = 1;
			var tf:TextField;
			
			for each (var o1:* in data.males) 
			{
				if(back["male_"+i]){
					tf = back["male_"+i] as TextField;
					tf.styleSheet = style;
					tf.htmlText="".concat("<a href='event:",o1.id,"'>",o1.fromUser,"</a>");
					tf.addEventListener(TextEvent.LINK,onLinkHandler);	
					feijiDict[o1.id] = o1;
				}
				i++;
			}
			
			var j:int = 1;
			for each (var o2:* in data.females) 
			{
				if(back["female_"+j]){
					tf = back["female_"+j] as TextField;
					tf.styleSheet = style;
					tf.htmlText="".concat("<a href='event:",o2.id,"'>",o2.fromUser,"</a>");
					tf.addEventListener(TextEvent.LINK,onLinkHandler);	
					feijiDict[o2.id] = o2;
				}
				j++;
			}
			
		}
		
		override public function show(event:Event=null):void
		{
			main.commu.send(OpCodes.READ_FEIJI_RANK, {}, rankCallback);
		}
		
		private function rankCallback(data:*):void
		{
			main.commu.unRegister(OpCodes.READ_FEIJI_RANK);
			if (data is String)
			{
				main.msg.showRollTipsMsg(data);
			}
			else
			{
				this.data=data;
				super.show();
			}
		}		
		
	}
}